package app.auto.runner;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.aliang.auto.R;

import app.auto.runner.base.ActivityPool;
import app.auto.runner.base.ActivityStack;
import app.auto.runner.base.action.ViewInflater;
import app.auto.runner.base.utility.BackStack;

/**
 * Created by Administrator on 2017/11/3.
 */
public class ActivityBaseCompact extends AppCompatActivity {


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityPool.currAty = this;
    }

    public Context getActvity() {
        return this;
    }


    public void touch(int search, View.OnTouchListener ontouchkListener) {
        getView(search).setOnTouchListener(ontouchkListener);
    }
    public void settag(View view, String tag) {
        view.setTag(tag);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (findViewById(R.id.title) != null) {

            findViewById(R.id.title).setOnLongClickListener(showNameLongclicklistener);
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            BackStack backStack = BackStack.map(this);
            if (backStack != null) {
                if (backStack.backPressed()) {
                    return false;
                }
            }
        }
        return super.onKeyDown(keyCode, event);

    }
    public static View.OnLongClickListener showNameLongclicklistener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            Toast.makeText(view.getContext(), ((Activity) view.getContext()).getClass().getSimpleName(), Toast.LENGTH_SHORT).show();
            return false;
        }
    };

    public void showToast(String toast) {
        Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
    }

    public String getTextString(int id) {
        return ((TextView) findViewById(id)).getText().toString();
    }

    public TextView getTextView(int id) {
        return (TextView) findViewById(id);
    }

    public EditText getEditText(int id) {
        return (EditText) findViewById(id);
    }

    public RadioButton getRadioButton(int id) {
        return (RadioButton) findViewById(id);
    }

    public ImageView getImageView(int id) {
        return (ImageView) findViewById(id);
    }

    public View getView(int id) {
        return findViewById(id);
    }

    public ViewGroup getGroup(int id) {
        return (ViewGroup) findViewById(id);
    }

    View workview;

    public ViewGroup getWorkview() {
        return (ViewGroup) workview;
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(workview = new ViewInflater(this).inflate(layoutResID, null));
    }

    public View getContentView() {
        return workview;
    }

    public void click(int id, View.OnClickListener clickListener) {
        getView(id).setOnClickListener(clickListener);
    }

    public void setOriContentView(int layoutResID) {
        setContentView(layoutResID);
    }


    public void setTextString(View view,int id,String t) {
        ((TextView) view.findViewById(id)).setText(t);
    }


    public void setTextString(int id,String t) {
        ((TextView) findViewById(id)).setText(t);
    }

}
